﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigTestV3.common;

namespace BigTestV3
{
    public partial class frmStuSet : Form
    {
        public frmStuSet()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置Ip地址事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtIP.Text.Trim()))
            {
                msgDiv.MsgDivShow("Ip地址不能为空");
            }
            else
            {
                _netFileTransferClient.NoticeRegisterMethodName = "tcp://" + txtIP.Text.Trim() + @":8085/FileService";
                msgDiv.MsgDivShow("设置成功");
            }
        }

        /// <summary>
        /// 从服务器端下载考试信息到本机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownloadClick(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtPath.Text.Trim()))
                {
                    msgDiv.MsgDivShow("路径不能空");
                }
                else
                {
                    if (!_netFileTransferClient.Connect())
                    {
                        _netFileTransferClient.Disconnection();
                        msgDiv.MsgDivShow("无法连接目标");
                    }
                    else
                    {
                        #region 从服务器端分别下载信息
                        string localPathSetting = FilePathTotal.SettingPath;
                        GetFileToLocal(txtPath.Text.Trim() + @"\Setting.xml", localPathSetting);

                        string localPathTest = FilePathTotal.TestInfoPath;
                        GetFileToLocal(txtPath.Text.Trim() + @"\TestInfo.xml", localPathTest);

                        string localPathPaper = FilePathTotal.StudentTestPath;
                        GetFileToLocal(txtPath.Text.Trim() + @"\StuTest.htm", localPathPaper);

                        string localMp3Path = FilePathTotal.Mp3Path;
                        GetFileToLocal(txtPath.Text.Trim() + @"\Listen.mp3", localMp3Path); 
                        #endregion

                        if (File.Exists(localPathPaper) && File.Exists(localPathSetting) 
                            && File.Exists(localPathTest) && File.Exists(localMp3Path))
                        {
                            msgDiv.MsgDivShow("下载成功");
                        }
                        else
                        {
                            msgDiv.MsgDivShow("下载失败");
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }


        }

        /// <summary>
        /// 从远程服务器端下载文件至本地路径
        /// </summary>
        /// <param name="remotpath">远程服务器端,包括IP，或端口等</param>
        /// <param name="localpath">本地路径</param>
        private void GetFileToLocal(string remotpath, string localpath)
        {
            _netFileTransferClient.GetFile(remotpath, localpath);
        }

        private void Play(object sender, EventArgs e)
        {
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = FilePathTotal.TestMp3Url;
            wplayer.controls.play();
        }
    }
}
